﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.Web.Central.Filters;
using Guline.Web.CentralModels.Object;
using WebMatrix.WebData;
namespace Guline.Web.Central.Controllers
{
    public class HomeController : BaseController
    {
        //[RequireHttps]//cái này để bắt Action hoặc controller nó đứng trên, đổi thành
        //https://  -- quan taam sau cai nay` :)
        public ActionResult Index()
        {
            if (User.Identity.IsAuthenticated)
                return RedirectToAction("Index", "Dashboard", new { area = "User" });

            List<Models.ListModel> lDay = new List<Models.ListModel>();
            for (int i = 1; i <= 31; i++)
            {
                lDay.Add(new Models.ListModel() { ID = i.ToString(), Name = i.ToString() });
            }
            ViewBag.Day = new SelectList(lDay, "ID", "Name");

            List<Models.ListModel> lMonth = new List<Models.ListModel>();
            for (int i = 1; i <= 12; i++)
            {
                lMonth.Add(new Models.ListModel() { ID = i.ToString(), Name = i.ToString() });
            }
            ViewBag.Month = new SelectList(lMonth, "ID", "Name");

            List<Models.ListModel> lYear = new List<Models.ListModel>();
            for (int i = DateTime.Now.Year - 5; i >= DateTime.Now.Year - 120; i--)
            {
                lYear.Add(new Models.ListModel() { ID = i.ToString(), Name = i.ToString() });
            }
            ViewBag.Year = new SelectList(lYear, "ID", "Name");
            Models.RegisterModel model = new Models.RegisterModel();
            model.Gender = true;
            return View(model);
        }
        [HttpPost]
        public ActionResult Index(Models.RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                Account acc = new Account();
                acc.BirthDay = new DateTime(model.Year, model.Month, model.Day);
                acc.DisplayName = model.FirstName + " " + model.LastName;
                acc.Email = model.Email;
                acc.FirstName = model.FirstName;
                acc.Gender = model.Gender;
                acc.LastName = model.LastName;
                acc.UserName = model.Email;
                WebSecurity.CreateUserAndAccount(model.Email, model.Password, new { acc.BirthDay, acc.DisplayName, acc.Email, acc.FirstName, acc.Gender, acc.LastName, acc.UserName });
                WebSecurity.Login(model.Email, model.Password);

                return RedirectToAction("Index", "Dashboard", new { area = "User" });

            }
            List<Models.ListModel> lDay = new List<Models.ListModel>();
            for (int i = 1; i <= 31; i++)
            {
                lDay.Add(new Models.ListModel() { ID = i.ToString(), Name = i.ToString() });
            }
            ViewBag.Day = new SelectList(lDay, "ID", "Name", model.Day);

            List<Models.ListModel> lMonth = new List<Models.ListModel>();
            for (int i = 1; i <= 12; i++)
            {
                lMonth.Add(new Models.ListModel() { ID = i.ToString(), Name = i.ToString() });
            }
            ViewBag.Month = new SelectList(lMonth, "ID", "Name", model.Month);

            List<Models.ListModel> lYear = new List<Models.ListModel>();
            for (int i = DateTime.Now.Year - 5; i >= DateTime.Now.Year - 120; i--)
            {
                lYear.Add(new Models.ListModel() { ID = i.ToString(), Name = i.ToString() });
            }
            ViewBag.Year = new SelectList(lYear, "ID", "Name", model.Year);

            return View(model);
        }

    }
}
